#include "db_handler.h"

DbHandler::DbHandler() {
	handler = NULL;
}

DbHandler::~DbHandler() {
	mysql_close(handler);
}

bool DbHandler::connect() {
	handler = mysql_init(handler);
	if (handler == NULL) return false;
	handler = mysql_real_connect(handler, "localhost", "root", 
			"bfezgl516", "gvj", 3306, NULL, 0);
	if (handler == NULL) return false;
	return true;
}

bool DbHandler::connect(const char *host, 
		const char *user, const char *passwd, const char *db, 
		unsigned int port, const char *unix_socket, 
		unsigned long client_flag) {
		
	handler = mysql_init(handler);
	if (handler == NULL) return false;
	handler = mysql_real_connect(handler, host, user, 
			passwd, db, port, unix_socket, client_flag);
	if (handler == NULL) return false;
	return true;
}

bool DbHandler::insert_access_record(const char *addr) {
	char sql[1024];
	sprintf(sql, "insert into access_record values(now(), %s)", addr);
	int res = mysql_query(handler, sql);
	return res;
}
